﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WAD.DAL.Admin;
using WAD.Model;
using GL.Tools;
using WAD.DAL;
using WAD.WEB.AppCode;

namespace DTcms.Web.admin.users
{
    public partial class user_edit : Page
    {
        protected int state = 0;
        protected decimal ad_balance, shared_balance;
        protected object area;
        protected void Page_Load(object sender, EventArgs e)
        {
            string name = Request.QueryString["name"].ToString();
            if (!IsPostBack)
            {
                getlist(name);
            }
        }

        public void getlist(string name)
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
                return;

            MUser mu = WAD.DAL.Admin.User.GetEntityById(Convert.ToInt32(Request.QueryString["id"]));
            if (mu != null)
            {
                txtUserName.Text = mu.name;
                hidUserName.Value = mu.name;
                txtNickName.Text = mu.nick_name;
                txtEmail.Text = mu.email;
                hdEmail.Value = mu.email;
                txtAvatar.Value = mu.logo;
                txtQQ.Text = mu.qq;
                txtMobile.Text = mu.phone;
                hdMobile.Value = mu.phone;
                parentId.Text = mu.parent_id.ToString();
                agentId.Text = mu.agentid.ToString();
                ad_balance = DaoUser.BalanceDncode(mu.ad_balance, WJXConfig.EncryptConfig.DES_KEY);
                shared_balance = DaoUser.BalanceDncode(mu.shared_balance, WJXConfig.EncryptConfig.DES_KEY);
                state = (mu.state == true ? 1 : 0);

                //区域
                Dictionary<string, object> setting = DAOFCSetting.Get("fc_setting", "");
                if (setting != null)
                {
                    if (setting["area1"] != DBNull.Value)
                        area = setting["area1"];
                    if (setting["area2"] != DBNull.Value)
                        area += "-" + setting["area2"];
                    if (setting["area3"] != DBNull.Value)
                        area += "-" + setting["area3"];
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MUser user = new MUser();
            if (hidUserName.Value != txtUserName.Text.Trim())
            {
                MUser mu = WAD.DAL.Admin.User.GetEntityByName(txtUserName.Text.Trim());
                if (mu != null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "Alert('用户名已经存在')", true);
                    return;
                }
                user.name = txtUserName.Text.Trim();
            }
            if (hdEmail.Value != txtEmail.Text.Trim())
            {
                MUser mu = WAD.DAL.Admin.User.GetEntityByName("", "email='" + txtEmail.Text.Trim() + "'");
                if (mu != null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "Alert('邮箱已注册')", true);
                    return;
                }
                user.email = txtEmail.Text;
            }
            if (hdMobile.Value != txtMobile.Text.Trim())
            {
                user.phone = txtMobile.Text.Trim();
                if (user.phone.Length > 0)
                {
                    MUser mu = WAD.DAL.Admin.User.GetEntityByName("", "phone='" + txtMobile.Text.Trim() + "'");
                    if (mu != null)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "Alert('手机号已注册')", true);
                        return;
                    }
                }

                //user.phone = txtMobile.Text.Trim();
            }
            if (txtPassword.Text.Trim() != "")
            {
                user.pwd = StringHelper.MD5Encrypt(txtPassword.Text);
            }
            user.id = Convert.ToInt32(Request.QueryString["id"].ToString());
            user.nick_name = txtNickName.Text.Trim();
            user.qq = txtQQ.Text;

            if (!string.IsNullOrEmpty(Request["state"]))
                user.state = (Request["state"] == "1" ? true : false);

            user.logo = txtAvatar.Value.Trim();
            user.parent_id = Convert.ToInt32(parentId.Text);
            user.agentid = Convert.ToInt32(agentId.Text);
            if (WAD.DAL.Admin.User.UpdateInfo(user) == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "Alert('保存成功')", true);
                getlist(txtUserName.Text.Trim());
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            getlist(Request.QueryString["name"].ToString());
        }


        #region JS提示============================================

        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        protected void JscriptMsg(string msgtitle, string url, string msgcss)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }

        /// <summary>
        /// 带回传函数的添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        /// <param name="callback">JS回调函数</param>
        protected void JscriptMsg(string msgtitle, string url, string msgcss, string callback)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\", " + callback + ")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
        #endregion
    }
}